﻿//
// (C) Copyright 2009 Irantha Suwandarathna (iranthas@hotmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EffiProz.Core.DataTypes;
using EffiProz.Core.Errors;

namespace EffiProz.Core
{
    public class SqlInvariants
    {
        /**
     * The role name reserved for authorization of INFORMATION_SCHEMA and
     * system objects.
     */
        public const String SYSTEM_AUTHORIZATION_NAME = "_SYSTEM";

        /** The role name reserved for ADMIN users. */
        public const String DBA_ADMIN_ROLE_NAME = "DBA";

        /** The role name allowing schema creation for users. */
        public const String SCHEMA_CREATE_ROLE_NAME = "CREATE_SCHEMA";

        /** The role name allowing switching authorisation for users. */
        public const String CHANGE_AUTH_ROLE_NAME = "CHANGE_AUTHORIZATION";

        //
        public const String SYSTEM_SUBQUERY = "SYSTEM_SUBQUERY";

        /** The role name reserved for the special PUBLIC pseudo-user. */
        public const String PUBLIC_ROLE_NAME = "PUBLIC";
        public const String SYSTEM_SCHEMA = "SYSTEM_SCHEMA";
        public const String LOBS_SCHEMA = "SYSTEM_LOBS";
        public const String DEFINITION_SCHEMA = "DEFINITION_SCHEMA";
        public const String INFORMATION_SCHEMA = "INFORMATION_SCHEMA";
        public const String SQLJ_SCHEMA = "SQLJ";
        public const String PUBLIC_SCHEMA = "PUBLIC";
        public const String CLASSPATH_NAME = "CLASSPATH";
        public static QNameManager.QName INFORMATION_SCHEMA_QNAME;
        public static QNameManager.QName SYSTEM_SCHEMA_QNAME;
        public static QNameManager.QName LOBS_SCHEMA_QNAME;
        public static QNameManager.QName SQLJ_SCHEMA_QNAME;
        public static QNameManager.QName SYSTEM_SUBQUERY_QNAME;

     
        public static Charset SQL_TEXT;
        public static Charset SQL_IDENTIFIER_CHARSET;
        public static Charset SQL_CHARACTER;
        public static Charset ASCII_GRAPHIC;    // == GRAPHIC_IRV
        public static Charset GRAPHIC_IRV;
        public static Charset ASCII_FULL;       // == ISO8BIT
        public static Charset ISO8BIT;
        public static Charset LATIN1;
        public static Charset UTF32;
        public static Charset UTF16;
        public static Charset UTF8;

        public static SqlType CARDINAL_NUMBER;
        public static SqlType YES_OR_NO;
        public static SqlType CHARACTER_DATA;
        public static SqlType SQL_IDENTIFIER;
        public static SqlType TIME_STAMP;

        static SqlInvariants()
        {
            INFORMATION_SCHEMA_QNAME =
               QNameManager.newSystemObjectName(INFORMATION_SCHEMA,
                   SchemaObjectTypes.SCHEMA);
            SYSTEM_SCHEMA_QNAME =
                QNameManager.newSystemObjectName(SYSTEM_SCHEMA,
                    SchemaObjectTypes.SCHEMA);
            LOBS_SCHEMA_QNAME = QNameManager.newSystemObjectName(LOBS_SCHEMA,
                    SchemaObjectTypes.SCHEMA);
            SQLJ_SCHEMA_QNAME = QNameManager.newSystemObjectName(SQLJ_SCHEMA,
                    SchemaObjectTypes.SCHEMA);
            SYSTEM_SUBQUERY_QNAME =
                QNameManager.newSystemObjectName(SYSTEM_SUBQUERY,
                    SchemaObjectTypes.TABLE);

            SYSTEM_SUBQUERY_QNAME.setSchemaIfNull(SYSTEM_SCHEMA_QNAME);

            QNameManager.QName name;

            name = QNameManager.newInfoSchemaObjectName("SQL_TEXT", false,
                    SchemaObjectTypes.CHARSET);
            SQL_TEXT = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("SQL_IDENTIFIER",
                    false, SchemaObjectTypes.CHARSET);
            SQL_IDENTIFIER_CHARSET = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("SQL_CHARACTER", false,
                    SchemaObjectTypes.CHARSET);
            SQL_CHARACTER = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("LATIN1", false,
                    SchemaObjectTypes.CHARSET);
            LATIN1 = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("ASCII_GRAPHIC", false,
                    SchemaObjectTypes.CHARSET);
            ASCII_GRAPHIC = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("GRAPHIC_IRV", false,
                    SchemaObjectTypes.CHARSET);
            GRAPHIC_IRV = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("ASCII_FULL", false,
                    SchemaObjectTypes.CHARSET);
            ASCII_FULL = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("ISO8BIT", false,
                    SchemaObjectTypes.CHARSET);
            ISO8BIT = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("UTF32", false,
                    SchemaObjectTypes.CHARSET);
            UTF32 = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("UTF16", false,
                    SchemaObjectTypes.CHARSET);
            UTF16 = new Charset(name);

            //
            name = QNameManager.newInfoSchemaObjectName("UTF8", false,
                    SchemaObjectTypes.CHARSET);
            UTF8 = new Charset(name);
            /*
             * Foundattion 4.2.1
             * Character sets defined by standards or by SQL-implementations reside
             * in the Information Schema (named INFORMATION_SCHEMA) in each catalog,
             * as do collations defined by standards and collations,
             * transliterations, and transcodings defined by SQL implementations.
             */
       
           
            name = QNameManager.newInfoSchemaObjectName("CARDINAL_NUMBER",
                    false, SchemaObjectTypes.DOMAIN);
            CARDINAL_NUMBER = new NumberType(Types.SQL_BIGINT, 0, 0);
            CARDINAL_NUMBER.userTypeModifier = new UserTypeModifier(name,
                    SchemaObjectTypes.DOMAIN, CARDINAL_NUMBER);

            //
            name = QNameManager.newInfoSchemaObjectName("YES_OR_NO", false,
                    SchemaObjectTypes.DOMAIN);
            YES_OR_NO = new CharacterType(Types.SQL_VARCHAR, 3);
            YES_OR_NO.userTypeModifier = new UserTypeModifier(name,
                    SchemaObjectTypes.DOMAIN, YES_OR_NO);

            //
            name = QNameManager.newInfoSchemaObjectName("CHARACTER_DATA",
                    false, SchemaObjectTypes.DOMAIN);
            CHARACTER_DATA = new CharacterType(Types.SQL_VARCHAR, (1 << 16));
            CHARACTER_DATA.userTypeModifier = new UserTypeModifier(name,
                    SchemaObjectTypes.DOMAIN, CHARACTER_DATA);

            //
            name = QNameManager.newInfoSchemaObjectName("SQL_IDENTIFIER",
                    false, SchemaObjectTypes.DOMAIN);
            SQL_IDENTIFIER = new CharacterType(Types.SQL_VARCHAR, 128);
            SQL_IDENTIFIER.userTypeModifier = new UserTypeModifier(name,
                    SchemaObjectTypes.DOMAIN, SQL_IDENTIFIER);


            name = QNameManager.newInfoSchemaObjectName("TIME_STAMP", false,
                    SchemaObjectTypes.DOMAIN);
            TIME_STAMP = new DateTimeType(Types.SQL_TIMESTAMP,
                                          Types.SQL_TIMESTAMP, 6);
            TIME_STAMP.userTypeModifier = new UserTypeModifier(name,
                    SchemaObjectTypes.DOMAIN, TIME_STAMP);
        }

        public static void checkSchemaNameNotSystem(String name)
        {

            if (isSchemaNameSystem(name))
            {
                throw Error.error(ErrorCode.X_42503, name);
            }
        }

        public static bool isSchemaNameSystem(String name)
        {

            if (SqlInvariants.DEFINITION_SCHEMA.Equals(name)
                    || SqlInvariants.INFORMATION_SCHEMA.Equals(name)
                    || SqlInvariants.SYSTEM_SCHEMA.Equals(name)
                    || SqlInvariants.SQLJ_SCHEMA.Equals(name))
            {
                return true;
            }

            return false;
        }

        public static bool isSchemaNameSystem(QNameManager.QName name)
        {

            if (name.schema != null)
            {
                name = name.schema;
            }

            if (SqlInvariants.INFORMATION_SCHEMA_QNAME.Equals(name)
                    || SqlInvariants.SYSTEM_SCHEMA_QNAME.Equals(name)
                    || SqlInvariants.SQLJ_SCHEMA_QNAME.Equals(name))
            {
                return true;
            }

            return false;
        }
    }
}
